<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%
	String path = request.getContextPath();
%>
<%@taglib prefix="shiro" uri="http://shiro.apache.org/tags" %>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>统计</title>


    
</head>

<body class="gray-bg">
    <div class="container-fluid">
	    <div class="row">
	       <div id="modalTitle" class="col-md-12" style="margin-top:30px;" modelId="${modelId}" >
	            <div class="col-md-4" >
	                 <h3 id="usedCoupon">已派发：0</h3>
	            </div>
	            <div class="col-md-4" >
	                 <h3 id="unUsedCoupon">剩余：0</h3>
	            </div>
	            <div class="col-md-4" >
	                 <h3 id="totalCoupon">总量：0</h3>
	            </div>
	       </div>
	    
           <div class="ibox-content">
                   <table class="table table-bordered">
                       <thead>
                           <tr>
                               <th>渠道</th>
                               <th>名称</th>
                               <th>派发数量</th>
                           </tr>
                       </thead>
                       <tbody id="tableBody">
                          
                       </tbody>
                   </table>

               </div>
          
           <div class="col-md-12 text-center" style="margin-bottom:20px;width:100%"> 
               <button onclick="javascript:colseCouponStatistics();" class="btn btn btn-primary">确认</button>
           </div>
           
        </div>
      
    </div>
   
	
    <script>
	    var path = "<%=path%>";
	    $(function(){
	    	var modelId=$("#modalTitle").attr("modelId");
	    	func_countCouponUsed(modelId);
	    	func_countCouponUsedDetail(modelId);
// 	    	couponStatistics(modelId);
	    });
        
	    function couponStatistics(modelId){
	   		var url=path+"/thirdpartyCouponModel/ajaxThirdpartyCouponStatistics";
          	$.post(url, {
  				"modelId":modelId,
  			}, function(data) {
  				if (data.status == 200) {
  					initHtml(data.data);
  				} else if(data.message=="20028"){
  					loginDialog();
  				}else {
  					errorDialog("获取数据失败",data.message);
  				}
  			});
	    }
	    
	    function initHtml(data){
	    	$('#usedCoupon').html("已派发："+data.useNum);
	    	$('#unUsedCoupon').html("剩余："+data.unUseNum);
	    	$('#totalCoupon').html("总量："+data.totalNum);
	    	var statisticsList=data.statisticsList;
	    	if(statisticsList.length>0){
	    		var html="";
	    		for(var i in statisticsList){
	    			html+='<tr><td>'+statisticsList[i].channelType+'</td>'
                    html+='<td>'+statisticsList[i].channelName+'</td>';
                    html+='<td>'+statisticsList[i].couponNum+'</td></tr>';
	    		}
	    		$('#tableBody').html(html);
	    	}
	    }
	    
	    
	    //统计第三方券使用与未使用的数量
	    function func_countCouponUsed(modelId){
	    	$.post(path+"/thirdpartyCoupon/ajaxThirdpartyCouponStatistics",{modelId:modelId},function(data){
	    		if(data.status == "200"){
	    			var usedNum = data.data.usedNum;
	    			var unusedNum = data.data.unusedNum;
	    			var totalCoupon = usedNum + unusedNum;
	    			$('#usedCoupon').html("已派发："+usedNum);
	    	    	$('#unUsedCoupon').html("剩余："+unusedNum);
	    	    	$('#totalCoupon').html("总量："+totalCoupon);
	    		}
	    	},"json");
	    }
	    
	    
	    //统计第三方券详细统计
	    function func_countCouponUsedDetail(modelId){
	    	$.post(path+"/thirdpartyCoupon/ajaxThirdpartyCouponStatisticsDetail",{modelId:modelId},function(data){
	    		if(data.status == "200"){
	    			var html="";
	    			$.each(data.data,function(i,d){
	    				if(d.usedNum > 0){
		    				html+='<tr><td>'+d.channelName+'</td>';
		                    html+='<td>'+d.name+'</td>';
		                    html+='<td>'+d.usedNum+'</td></tr>';
	    				}
	    			});
		    		$('#tableBody').html(html);
	    		}
	    	},"json");
	    }
	    
	    function colseCouponStatistics(){
	    	$('#couponStatistics').modal('hide');
	    	//清楚模态框内容
	    	$("#couponStatistics").on("hidden.bs.modal", function() {
	    	    $(this).removeData("bs.modal");
	    	});
	    }
    </script>
</body>
</html>